<?php
/**
 * Created by 784855684@qq.com.
 * Link: https://github.com/lichtung/wshore
 * User: linzh
 * Date: 7/8/17
 * Time: 11:30 AM
 */
declare(strict_types=1);


namespace application\module\admin\addon\wechat\model;


use application\module\admin\addon\model\AddonModel;
use application\module\admin\addon\wechat\entity\Message;

/**
 * Class MessageModel
 *
 * @property string $to         消息发送者openid
 * @property string $from       消息接受者,即公众号ID
 * @property string $sendtime   发送时间
 * @property string $type       消息类型,目前支持text,image,event
 * @property string $msgid      消息ID
 * @property string $content    消息内容,图片消息类型时为存放路径
 *
 * @property string $mediaId    媒体ID
 * @property string $picUrl     图片消息URL
 * @property string $looked     消息是否查看
 *
 *
 * @package application\module\admin\addon\wechat\model
 */
class MessageModel extends AddonModel
{
    protected function tablePrefix()
    {
        return 'ws_';
    }

    protected function tableName()
    {
        return 'wechat_message';
    }

    public function __construct($id = null)
    {
        if (is_numeric($id)) {
            parent::__construct($id);
        } elseif ($id and $id instanceof Message) {
            parent::__construct();
            /** @var Message $message */
            $message = &$id;
            $this->to = $message->to;
            $this->from = $message->from;
            $this->sendtime = $message->sendtime;
            $this->type = $message->type;
            $this->msgid = $message->msgid;
            $this->content = $message->content;
            $this->mediaId = $message->mediaId;
            $this->picUrl = $message->picUrl;
            $this->looked = 0;
            $this->save();
        } else {
            parent::__construct();
            # to create
        }
    }

    protected function struct(): string
    {
        return "  `id` int(11) NOT NULL AUTO_INCREMENT,
  `to` varchar(32) CHARACTER SET ascii NOT NULL COMMENT '开发者微信号',
  `from` varchar(40) CHARACTER SET ascii NOT NULL COMMENT '发送方帐号（OpenID）',
  `sendtime` int(11) unsigned NOT NULL COMMENT '发送时间',
  `type` varchar(10) CHARACTER SET ascii NOT NULL COMMENT '消息类型，text,image...',
  `msgid` varchar(255) NOT NULL,
  `content` varchar(255) DEFAULT NULL COMMENT '文本消息内容',
  `mediaId` varchar(255) DEFAULT NULL COMMENT '图片消息媒体id，可以调用多媒体文件下载接口拉取数据。',
  `picUrl` varchar(255) DEFAULT NULL COMMENT '图片链接（由系统生成）',
  `looked` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '读取时间,未读取为0',
  PRIMARY KEY (`id`)";
    }
}